<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 15 章 数据存储 | Python 编程</title>
  <meta name="description" content="使用Python进行编程" />
  <meta name="generator" content="bookdown 0.17.2 and GitBook 2.6.7" />

  <meta property="og:title" content="第 15 章 数据存储 | Python 编程" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="使用Python进行编程" />
  <meta name="github-repo" content="yangjh-xbmu/learningpython" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 15 章 数据存储 | Python 编程" />
  
  <meta name="twitter:description" content="使用Python进行编程" />
  

<meta name="author" content="杨志宏" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="pyquery.html"/>
<link rel="next" href="selenium.html"/>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css\style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">LearningPython</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Python 简介</a><ul>
<li class="chapter" data-level="0.1" data-path="index.html"><a href="index.html#python-发展历史"><i class="fa fa-check"></i><b>0.1</b> Python 发展历史</a></li>
<li class="chapter" data-level="0.2" data-path="index.html"><a href="index.html#python-特点"><i class="fa fa-check"></i><b>0.2</b> Python 特点</a></li>
<li class="chapter" data-level="0.3" data-path="index.html"><a href="index.html#使用-python-的知名项目"><i class="fa fa-check"></i><b>0.3</b> 使用 Python 的知名项目</a></li>
<li class="chapter" data-level="0.4" data-path="index.html"><a href="index.html#学习资源"><i class="fa fa-check"></i><b>0.4</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="ide.html"><a href="ide.html"><i class="fa fa-check"></i><b>1</b> Python 开发环境搭建</a><ul>
<li class="chapter" data-level="1.1" data-path="ide.html"><a href="ide.html#在-windows-中安装-python-3"><i class="fa fa-check"></i><b>1.1</b> 在 Windows 中安装 Python 3</a></li>
<li class="chapter" data-level="1.2" data-path="ide.html"><a href="ide.html#在-macos-中安装-python-3"><i class="fa fa-check"></i><b>1.2</b> 在 MacOS 中安装 Python 3</a><ul>
<li class="chapter" data-level="1.2.1" data-path="ide.html"><a href="ide.html#安装-xcode"><i class="fa fa-check"></i><b>1.2.1</b> 安装 Xcode</a></li>
<li class="chapter" data-level="1.2.2" data-path="ide.html"><a href="ide.html#安装-homebrew"><i class="fa fa-check"></i><b>1.2.2</b> 安装 Homebrew</a></li>
<li class="chapter" data-level="1.2.3" data-path="ide.html"><a href="ide.html#安装-python3"><i class="fa fa-check"></i><b>1.2.3</b> 安装 Python3</a></li>
<li class="chapter" data-level="1.2.4" data-path="ide.html"><a href="ide.html#使用-python3"><i class="fa fa-check"></i><b>1.2.4</b> 使用 Python3</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="ide.html"><a href="ide.html#使用-python-虚拟环境"><i class="fa fa-check"></i><b>1.3</b> 使用 python 虚拟环境</a><ul>
<li class="chapter" data-level="1.3.1" data-path="ide.html"><a href="ide.html#为什么要用虚拟环境"><i class="fa fa-check"></i><b>1.3.1</b> 为什么要用虚拟环境</a></li>
<li class="chapter" data-level="1.3.2" data-path="ide.html"><a href="ide.html#虚拟环境的创建与使用"><i class="fa fa-check"></i><b>1.3.2</b> 虚拟环境的创建与使用</a></li>
<li class="chapter" data-level="1.3.3" data-path="ide.html"><a href="ide.html#使用-pipenv-管理虚拟环境"><i class="fa fa-check"></i><b>1.3.3</b> 使用 pipenv 管理虚拟环境</a></li>
<li class="chapter" data-level="1.3.4" data-path="ide.html"><a href="ide.html#虚拟环境与系统环境的区别"><i class="fa fa-check"></i><b>1.3.4</b> 虚拟环境与系统环境的区别</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="ide.html"><a href="ide.html#编辑器"><i class="fa fa-check"></i><b>1.4</b> 编辑器</a><ul>
<li class="chapter" data-level="1.4.1" data-path="ide.html"><a href="ide.html#visual-studio-code-中的必要设置"><i class="fa fa-check"></i><b>1.4.1</b> Visual Studio Code 中的必要设置</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>I 核心语法</b></span></li>
<li class="chapter" data-level="2" data-path="systax.html"><a href="systax.html"><i class="fa fa-check"></i><b>2</b> 核心语法</a><ul>
<li class="chapter" data-level="2.1" data-path="systax.html"><a href="systax.html#注释"><i class="fa fa-check"></i><b>2.1</b> 注释</a></li>
<li class="chapter" data-level="2.2" data-path="systax.html"><a href="systax.html#变量"><i class="fa fa-check"></i><b>2.2</b> 变量</a><ul>
<li class="chapter" data-level="2.2.1" data-path="systax.html"><a href="systax.html#变量名称"><i class="fa fa-check"></i><b>2.2.1</b> 变量名称</a></li>
<li class="chapter" data-level="2.2.2" data-path="systax.html"><a href="systax.html#变量赋值"><i class="fa fa-check"></i><b>2.2.2</b> 变量赋值</a></li>
<li class="chapter" data-level="2.2.3" data-path="systax.html"><a href="systax.html#同步赋值"><i class="fa fa-check"></i><b>2.2.3</b> 同步赋值</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="systax.html"><a href="systax.html#数字类型"><i class="fa fa-check"></i><b>2.3</b> 数字类型</a><ul>
<li class="chapter" data-level="2.3.1" data-path="systax.html"><a href="systax.html#查看变量类型"><i class="fa fa-check"></i><b>2.3.1</b> 查看变量类型</a></li>
<li class="chapter" data-level="2.3.2" data-path="systax.html"><a href="systax.html#整型"><i class="fa fa-check"></i><b>2.3.2</b> 整型</a></li>
<li class="chapter" data-level="2.3.3" data-path="systax.html"><a href="systax.html#浮点类型"><i class="fa fa-check"></i><b>2.3.3</b> 浮点类型</a></li>
<li class="chapter" data-level="2.3.4" data-path="systax.html"><a href="systax.html#复数"><i class="fa fa-check"></i><b>2.3.4</b> 复数</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="systax.html"><a href="systax.html#运算符"><i class="fa fa-check"></i><b>2.4</b> 运算符</a><ul>
<li class="chapter" data-level="2.4.1" data-path="systax.html"><a href="systax.html#运算符的优先级别"><i class="fa fa-check"></i><b>2.4.1</b> 运算符的优先级别</a></li>
<li class="chapter" data-level="2.4.2" data-path="systax.html"><a href="systax.html#增强赋值运算符"><i class="fa fa-check"></i><b>2.4.2</b> 增强赋值运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="systax.html"><a href="systax.html#序列"><i class="fa fa-check"></i><b>2.5</b> 序列</a><ul>
<li class="chapter" data-level="2.5.1" data-path="systax.html"><a href="systax.html#索引"><i class="fa fa-check"></i><b>2.5.1</b> 索引</a></li>
<li class="chapter" data-level="2.5.2" data-path="systax.html"><a href="systax.html#分片"><i class="fa fa-check"></i><b>2.5.2</b> 分片</a></li>
<li class="chapter" data-level="2.5.3" data-path="systax.html"><a href="systax.html#序列相加"><i class="fa fa-check"></i><b>2.5.3</b> 序列相加</a></li>
<li class="chapter" data-level="2.5.4" data-path="systax.html"><a href="systax.html#序列相乘"><i class="fa fa-check"></i><b>2.5.4</b> 序列相乘</a></li>
<li class="chapter" data-level="2.5.5" data-path="systax.html"><a href="systax.html#成员资格"><i class="fa fa-check"></i><b>2.5.5</b> 成员资格</a></li>
<li class="chapter" data-level="2.5.6" data-path="systax.html"><a href="systax.html#长度最小值最大值"><i class="fa fa-check"></i><b>2.5.6</b> 长度、最小值、最大值</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="systax.html"><a href="systax.html#字符串"><i class="fa fa-check"></i><b>2.6</b> 字符串</a><ul>
<li class="chapter" data-level="2.6.1" data-path="systax.html"><a href="systax.html#创建字符串"><i class="fa fa-check"></i><b>2.6.1</b> 创建字符串</a></li>
<li class="chapter" data-level="2.6.2" data-path="systax.html"><a href="systax.html#字符串的不可变性"><i class="fa fa-check"></i><b>2.6.2</b> 字符串的不可变性</a></li>
<li class="chapter" data-level="2.6.3" data-path="systax.html"><a href="systax.html#字符串操作"><i class="fa fa-check"></i><b>2.6.3</b> 字符串操作</a></li>
<li class="chapter" data-level="2.6.4" data-path="systax.html"><a href="systax.html#字符串分片"><i class="fa fa-check"></i><b>2.6.4</b> 字符串分片</a></li>
<li class="chapter" data-level="2.6.5" data-path="systax.html"><a href="systax.html#in-和-not-in-操作符"><i class="fa fa-check"></i><b>2.6.5</b> in 和 not in 操作符</a></li>
<li class="chapter" data-level="2.6.6" data-path="systax.html"><a href="systax.html#string-对象的方法"><i class="fa fa-check"></i><b>2.6.6</b> String 对象的方法</a></li>
<li class="chapter" data-level="2.6.7" data-path="systax.html"><a href="systax.html#比较字符串"><i class="fa fa-check"></i><b>2.6.7</b> 比较字符串</a></li>
<li class="chapter" data-level="2.6.8" data-path="systax.html"><a href="systax.html#遍历字符串"><i class="fa fa-check"></i><b>2.6.8</b> 遍历字符串</a></li>
<li class="chapter" data-level="2.6.9" data-path="systax.html"><a href="systax.html#字符串内容检验"><i class="fa fa-check"></i><b>2.6.9</b> 字符串内容检验</a></li>
<li class="chapter" data-level="2.6.10" data-path="systax.html"><a href="systax.html#在字符串内查找和替换"><i class="fa fa-check"></i><b>2.6.10</b> 在字符串内查找和替换</a></li>
</ul></li>
<li class="chapter" data-level="2.7" data-path="systax.html"><a href="systax.html#列表"><i class="fa fa-check"></i><b>2.7</b> 列表</a><ul>
<li class="chapter" data-level="2.7.1" data-path="systax.html"><a href="systax.html#列表赋值"><i class="fa fa-check"></i><b>2.7.1</b> 列表赋值</a></li>
<li class="chapter" data-level="2.7.2" data-path="systax.html"><a href="systax.html#删除元素"><i class="fa fa-check"></i><b>2.7.2</b> 删除元素</a></li>
<li class="chapter" data-level="2.7.3" data-path="systax.html"><a href="systax.html#分片赋值"><i class="fa fa-check"></i><b>2.7.3</b> 分片赋值</a></li>
<li class="chapter" data-level="2.7.4" data-path="systax.html"><a href="systax.html#列表对象常用内置方法"><i class="fa fa-check"></i><b>2.7.4</b> 列表对象常用内置方法</a></li>
</ul></li>
<li class="chapter" data-level="2.8" data-path="systax.html"><a href="systax.html#字典"><i class="fa fa-check"></i><b>2.8</b> 字典</a><ul>
<li class="chapter" data-level="2.8.1" data-path="systax.html"><a href="systax.html#创建字典"><i class="fa fa-check"></i><b>2.8.1</b> 创建字典</a></li>
<li class="chapter" data-level="2.8.2" data-path="systax.html"><a href="systax.html#获取修改和添加字典元素"><i class="fa fa-check"></i><b>2.8.2</b> 获取、修改和添加字典元素</a></li>
<li class="chapter" data-level="2.8.3" data-path="systax.html"><a href="systax.html#遍历字典"><i class="fa fa-check"></i><b>2.8.3</b> 遍历字典</a></li>
<li class="chapter" data-level="2.8.4" data-path="systax.html"><a href="systax.html#字典常用方法"><i class="fa fa-check"></i><b>2.8.4</b> 字典常用方法</a></li>
<li class="chapter" data-level="2.8.5" data-path="systax.html"><a href="systax.html#字典的排序"><i class="fa fa-check"></i><b>2.8.5</b> 字典的排序</a></li>
</ul></li>
<li class="chapter" data-level="2.9" data-path="systax.html"><a href="systax.html#元组"><i class="fa fa-check"></i><b>2.9</b> 元组</a><ul>
<li class="chapter" data-level="2.9.1" data-path="systax.html"><a href="systax.html#创建元组"><i class="fa fa-check"></i><b>2.9.1</b> 创建元组</a></li>
<li class="chapter" data-level="2.9.2" data-path="systax.html"><a href="systax.html#元组相关方法"><i class="fa fa-check"></i><b>2.9.2</b> 元组相关方法</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="systax.html"><a href="systax.html#控制声明"><i class="fa fa-check"></i><b>2.10</b> 控制声明</a><ul>
<li class="chapter" data-level="2.10.1" data-path="systax.html"><a href="systax.html#分支判断"><i class="fa fa-check"></i><b>2.10.1</b> 分支判断</a></li>
<li class="chapter" data-level="2.10.2" data-path="systax.html"><a href="systax.html#分支嵌套"><i class="fa fa-check"></i><b>2.10.2</b> 分支嵌套</a></li>
<li class="chapter" data-level="2.10.3" data-path="systax.html"><a href="systax.html#三元运算符"><i class="fa fa-check"></i><b>2.10.3</b> 三元运算符</a></li>
</ul></li>
<li class="chapter" data-level="2.11" data-path="systax.html"><a href="systax.html#循环"><i class="fa fa-check"></i><b>2.11</b> 循环</a><ul>
<li class="chapter" data-level="2.11.1" data-path="systax.html"><a href="systax.html#for-循环"><i class="fa fa-check"></i><b>2.11.1</b> for 循环</a></li>
<li class="chapter" data-level="2.11.2" data-path="systax.html"><a href="systax.html#范围循环"><i class="fa fa-check"></i><b>2.11.2</b> 范围循环</a></li>
<li class="chapter" data-level="2.11.3" data-path="systax.html"><a href="systax.html#while-循环"><i class="fa fa-check"></i><b>2.11.3</b> while 循环</a></li>
<li class="chapter" data-level="2.11.4" data-path="systax.html"><a href="systax.html#中断循环"><i class="fa fa-check"></i><b>2.11.4</b> 中断循环</a></li>
<li class="chapter" data-level="2.11.5" data-path="systax.html"><a href="systax.html#继续循环"><i class="fa fa-check"></i><b>2.11.5</b> 继续循环</a></li>
</ul></li>
<li class="chapter" data-level="2.12" data-path="systax.html"><a href="systax.html#函数"><i class="fa fa-check"></i><b>2.12</b> 函数</a><ul>
<li class="chapter" data-level="2.12.1" data-path="systax.html"><a href="systax.html#创建函数"><i class="fa fa-check"></i><b>2.12.1</b> 创建函数</a></li>
<li class="chapter" data-level="2.12.2" data-path="systax.html"><a href="systax.html#函数返回值"><i class="fa fa-check"></i><b>2.12.2</b> 函数返回值</a></li>
<li class="chapter" data-level="2.12.3" data-path="systax.html"><a href="systax.html#全局变量和局域变量"><i class="fa fa-check"></i><b>2.12.3</b> 全局变量和局域变量</a></li>
<li class="chapter" data-level="2.12.4" data-path="systax.html"><a href="systax.html#参数的默认值"><i class="fa fa-check"></i><b>2.12.4</b> 参数的默认值</a></li>
<li class="chapter" data-level="2.12.5" data-path="systax.html"><a href="systax.html#关键字参数"><i class="fa fa-check"></i><b>2.12.5</b> 关键字参数</a></li>
<li class="chapter" data-level="2.12.6" data-path="systax.html"><a href="systax.html#返回多个值"><i class="fa fa-check"></i><b>2.12.6</b> 返回多个值</a></li>
<li class="chapter" data-level="2.12.7" data-path="systax.html"><a href="systax.html#函数文档字符串"><i class="fa fa-check"></i><b>2.12.7</b> 函数文档字符串</a></li>
<li class="chapter" data-level="2.12.8" data-path="systax.html"><a href="systax.html#lambda表达式"><i class="fa fa-check"></i><b>2.12.8</b> lambda表达式</a></li>
<li class="chapter" data-level="2.12.9" data-path="systax.html"><a href="systax.html#args-和-kwargs"><i class="fa fa-check"></i><b>2.12.9</b> <code>*args</code> 和 <code>**kwargs</code></a></li>
<li class="chapter" data-level="2.12.10" data-path="systax.html"><a href="systax.html#参考资料"><i class="fa fa-check"></i><b>2.12.10</b> 参考资料</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="oop.html"><a href="oop.html"><i class="fa fa-check"></i><b>3</b> Python中的面向对象编程</a><ul>
<li class="chapter" data-level="3.1" data-path="oop.html"><a href="oop.html#python对象和类"><i class="fa fa-check"></i><b>3.1</b> Python对象和类</a><ul>
<li class="chapter" data-level="3.1.1" data-path="oop.html"><a href="oop.html#创建类"><i class="fa fa-check"></i><b>3.1.1</b> 创建类</a></li>
<li class="chapter" data-level="3.1.2" data-path="oop.html"><a href="oop.html#从类中创建对象"><i class="fa fa-check"></i><b>3.1.2</b> 从类中创建对象</a></li>
<li class="chapter" data-level="3.1.3" data-path="oop.html"><a href="oop.html#隐藏数据字段"><i class="fa fa-check"></i><b>3.1.3</b> 隐藏数据字段</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="oop.html"><a href="oop.html#操作符重载"><i class="fa fa-check"></i><b>3.2</b> 操作符重载</a></li>
<li class="chapter" data-level="3.3" data-path="oop.html"><a href="oop.html#继承和多态"><i class="fa fa-check"></i><b>3.3</b> 继承和多态</a><ul>
<li class="chapter" data-level="3.3.1" data-path="oop.html"><a href="oop.html#多重继承"><i class="fa fa-check"></i><b>3.3.1</b> 多重继承</a></li>
<li class="chapter" data-level="3.3.2" data-path="oop.html"><a href="oop.html#重写方法"><i class="fa fa-check"></i><b>3.3.2</b> 重写方法</a></li>
<li class="chapter" data-level="3.3.3" data-path="oop.html"><a href="oop.html#判断对象是否属于某类"><i class="fa fa-check"></i><b>3.3.3</b> 判断对象是否属于某类</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="exception.html"><a href="exception.html"><i class="fa fa-check"></i><b>4</b> 异常处理</a><ul>
<li class="chapter" data-level="4.1" data-path="exception.html"><a href="exception.html#捕获异常"><i class="fa fa-check"></i><b>4.1</b> 捕获异常</a><ul>
<li class="chapter" data-level="4.1.1" data-path="exception.html"><a href="exception.html#try-except"><i class="fa fa-check"></i><b>4.1.1</b> try-except</a></li>
<li class="chapter" data-level="4.1.2" data-path="exception.html"><a href="exception.html#多个except"><i class="fa fa-check"></i><b>4.1.2</b> 多个except</a></li>
<li class="chapter" data-level="4.1.3" data-path="exception.html"><a href="exception.html#自定义异常"><i class="fa fa-check"></i><b>4.1.3</b> 自定义异常</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="5" data-path="decorator.html"><a href="decorator.html"><i class="fa fa-check"></i><b>5</b> Python 装饰器</a><ul>
<li class="chapter" data-level="5.1" data-path="decorator.html"><a href="decorator.html#为什么要用装饰器"><i class="fa fa-check"></i><b>5.1</b> 为什么要用装饰器</a></li>
<li class="chapter" data-level="5.2" data-path="decorator.html"><a href="decorator.html#简单装饰器"><i class="fa fa-check"></i><b>5.2</b> 简单装饰器</a></li>
<li class="chapter" data-level="5.3" data-path="decorator.html"><a href="decorator.html#语法糖"><i class="fa fa-check"></i><b>5.3</b> @ 语法糖</a></li>
<li class="chapter" data-level="5.4" data-path="decorator.html"><a href="decorator.html#argskwargs"><i class="fa fa-check"></i><b>5.4</b> <code>*args</code>、<code>**kwargs</code></a></li>
<li class="chapter" data-level="5.5" data-path="decorator.html"><a href="decorator.html#带参数的装饰器"><i class="fa fa-check"></i><b>5.5</b> 带参数的装饰器</a></li>
<li class="chapter" data-level="5.6" data-path="decorator.html"><a href="decorator.html#类装饰器"><i class="fa fa-check"></i><b>5.6</b> 类装饰器</a></li>
<li class="chapter" data-level="5.7" data-path="decorator.html"><a href="decorator.html#functools.wraps"><i class="fa fa-check"></i><b>5.7</b> functools.wraps</a></li>
<li class="chapter" data-level="5.8" data-path="decorator.html"><a href="decorator.html#装饰器顺序"><i class="fa fa-check"></i><b>5.8</b> 装饰器顺序</a></li>
<li class="chapter" data-level="" data-path="decorator.html"><a href="decorator.html#参考资料-1"><i class="fa fa-check"></i>参考资料</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="module.html"><a href="module.html"><i class="fa fa-check"></i><b>6</b> 模块</a><ul>
<li class="chapter" data-level="6.1" data-path="module.html"><a href="module.html#创建模块"><i class="fa fa-check"></i><b>6.1</b> 创建模块</a></li>
<li class="chapter" data-level="6.2" data-path="module.html"><a href="module.html#使用模块中的指定内容"><i class="fa fa-check"></i><b>6.2</b> 使用模块中的指定内容</a></li>
<li class="chapter" data-level="6.3" data-path="module.html"><a href="module.html#dir函数"><i class="fa fa-check"></i><b>6.3</b> dir函数</a></li>
<li class="chapter" data-level="6.4" data-path="module.html"><a href="module.html#包"><i class="fa fa-check"></i><b>6.4</b> 包</a></li>
</ul></li>
<li class="part"><span><b>II 进阶</b></span></li>
<li class="chapter" data-level="7" data-path="buildinfunctions.html"><a href="buildinfunctions.html"><i class="fa fa-check"></i><b>7</b> 内置函数</a><ul>
<li class="chapter" data-level="7.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#dict函数"><i class="fa fa-check"></i><b>7.1</b> dict函数</a><ul>
<li class="chapter" data-level="7.1.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述"><i class="fa fa-check"></i><b>7.1.1</b> 描述</a></li>
<li class="chapter" data-level="7.1.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法"><i class="fa fa-check"></i><b>7.1.2</b> 语法</a></li>
<li class="chapter" data-level="7.1.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值"><i class="fa fa-check"></i><b>7.1.3</b> 返回值</a></li>
<li class="chapter" data-level="7.1.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例"><i class="fa fa-check"></i><b>7.1.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#zip函数"><i class="fa fa-check"></i><b>7.2</b> zip函数</a><ul>
<li class="chapter" data-level="7.2.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-1"><i class="fa fa-check"></i><b>7.2.1</b> 描述</a></li>
<li class="chapter" data-level="7.2.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-1"><i class="fa fa-check"></i><b>7.2.2</b> 语法</a></li>
<li class="chapter" data-level="7.2.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-1"><i class="fa fa-check"></i><b>7.2.3</b> 返回值</a></li>
<li class="chapter" data-level="7.2.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#特殊用法"><i class="fa fa-check"></i><b>7.2.4</b> 特殊用法</a></li>
<li class="chapter" data-level="7.2.5" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-1"><i class="fa fa-check"></i><b>7.2.5</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#list函数"><i class="fa fa-check"></i><b>7.3</b> list函数</a><ul>
<li class="chapter" data-level="7.3.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-2"><i class="fa fa-check"></i><b>7.3.1</b> 描述</a></li>
<li class="chapter" data-level="7.3.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-2"><i class="fa fa-check"></i><b>7.3.2</b> 语法</a></li>
<li class="chapter" data-level="7.3.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-2"><i class="fa fa-check"></i><b>7.3.3</b> 返回值</a></li>
<li class="chapter" data-level="7.3.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-2"><i class="fa fa-check"></i><b>7.3.4</b> 实例</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#min函数"><i class="fa fa-check"></i><b>7.4</b> min函数</a><ul>
<li class="chapter" data-level="7.4.1" data-path="buildinfunctions.html"><a href="buildinfunctions.html#描述-3"><i class="fa fa-check"></i><b>7.4.1</b> 描述</a></li>
<li class="chapter" data-level="7.4.2" data-path="buildinfunctions.html"><a href="buildinfunctions.html#语法-3"><i class="fa fa-check"></i><b>7.4.2</b> 语法</a></li>
<li class="chapter" data-level="7.4.3" data-path="buildinfunctions.html"><a href="buildinfunctions.html#返回值-3"><i class="fa fa-check"></i><b>7.4.3</b> 返回值</a></li>
<li class="chapter" data-level="7.4.4" data-path="buildinfunctions.html"><a href="buildinfunctions.html#实例-3"><i class="fa fa-check"></i><b>7.4.4</b> 实例</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="iterator.html"><a href="iterator.html"><i class="fa fa-check"></i><b>8</b> Python可迭代对象</a><ul>
<li class="chapter" data-level="8.1" data-path="iterator.html"><a href="iterator.html#可迭代对象"><i class="fa fa-check"></i><b>8.1</b> 可迭代对象</a></li>
<li class="chapter" data-level="8.2" data-path="iterator.html"><a href="iterator.html#迭代器"><i class="fa fa-check"></i><b>8.2</b> 迭代器</a></li>
<li class="chapter" data-level="8.3" data-path="iterator.html"><a href="iterator.html#生成器"><i class="fa fa-check"></i><b>8.3</b> 生成器</a></li>
<li class="chapter" data-level="8.4" data-path="iterator.html"><a href="iterator.html#三者之间关系"><i class="fa fa-check"></i><b>8.4</b> 三者之间关系</a></li>
<li class="chapter" data-level="8.5" data-path="iterator.html"><a href="iterator.html#迭代器长度的计算"><i class="fa fa-check"></i><b>8.5</b> 迭代器长度的计算</a></li>
<li class="chapter" data-level="8.6" data-path="iterator.html"><a href="iterator.html#yield"><i class="fa fa-check"></i><b>8.6</b> yield</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="underline.html"><a href="underline.html"><i class="fa fa-check"></i><b>9</b> 单、双下划线的区别</a><ul>
<li class="chapter" data-level="9.1" data-path="underline.html"><a href="underline.html#单下划线开头"><i class="fa fa-check"></i><b>9.1</b> 单下划线开头</a></li>
<li class="chapter" data-level="9.2" data-path="underline.html"><a href="underline.html#双下划线开头"><i class="fa fa-check"></i><b>9.2</b> 双下划线开头</a></li>
<li class="chapter" data-level="9.3" data-path="underline.html"><a href="underline.html#双下划线开头和结尾"><i class="fa fa-check"></i><b>9.3</b> 双下划线开头和结尾</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="re.html"><a href="re.html"><i class="fa fa-check"></i><b>10</b> 正则表达式</a><ul>
<li class="chapter" data-level="10.1" data-path="re.html"><a href="re.html#正则表达式发展简史"><i class="fa fa-check"></i><b>10.1</b> 正则表达式发展简史</a></li>
<li class="chapter" data-level="10.2" data-path="re.html"><a href="re.html#python中使用正则表达式的流程"><i class="fa fa-check"></i><b>10.2</b> Python中使用正则表达式的流程</a></li>
<li class="chapter" data-level="10.3" data-path="re.html"><a href="re.html#正则表达式的构成要素"><i class="fa fa-check"></i><b>10.3</b> 正则表达式的构成要素</a><ul>
<li class="chapter" data-level="10.3.1" data-path="re.html"><a href="re.html#定界符"><i class="fa fa-check"></i><b>10.3.1</b> 定界符</a></li>
<li class="chapter" data-level="10.3.2" data-path="re.html"><a href="re.html#原子"><i class="fa fa-check"></i><b>10.3.2</b> 原子</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="re.html"><a href="re.html#元字符"><i class="fa fa-check"></i><b>10.4</b> 元字符</a><ul>
<li class="chapter" data-level="10.4.1" data-path="re.html"><a href="re.html#匹配单个字符的元字符"><i class="fa fa-check"></i><b>10.4.1</b> 匹配单个字符的元字符</a></li>
<li class="chapter" data-level="10.4.2" data-path="re.html"><a href="re.html#常用转义符"><i class="fa fa-check"></i><b>10.4.2</b> 常用转义符</a></li>
<li class="chapter" data-level="10.4.3" data-path="re.html"><a href="re.html#提供计数功能的元字符"><i class="fa fa-check"></i><b>10.4.3</b> 提供计数功能的元字符</a></li>
<li class="chapter" data-level="10.4.4" data-path="re.html"><a href="re.html#匹配位置的元字符"><i class="fa fa-check"></i><b>10.4.4</b> 匹配位置的元字符</a></li>
<li class="chapter" data-level="10.4.5" data-path="re.html"><a href="re.html#其他元字符"><i class="fa fa-check"></i><b>10.4.5</b> 其他元字符</a></li>
</ul></li>
<li class="chapter" data-level="10.5" data-path="re.html"><a href="re.html#模式修饰符"><i class="fa fa-check"></i><b>10.5</b> 模式修饰符</a></li>
<li class="chapter" data-level="10.6" data-path="re.html"><a href="re.html#在python中使用正则表达式"><i class="fa fa-check"></i><b>10.6</b> 在Python中使用正则表达式</a><ul>
<li class="chapter" data-level="10.6.1" data-path="re.html"><a href="re.html#search"><i class="fa fa-check"></i><b>10.6.1</b> search()</a></li>
<li class="chapter" data-level="10.6.2" data-path="re.html"><a href="re.html#compile"><i class="fa fa-check"></i><b>10.6.2</b> compile()</a></li>
<li class="chapter" data-level="10.6.3" data-path="re.html"><a href="re.html#findall"><i class="fa fa-check"></i><b>10.6.3</b> findall()</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="11" data-path="argparse.html"><a href="argparse.html"><i class="fa fa-check"></i><b>11</b> 命令行参数模块argparse</a><ul>
<li class="chapter" data-level="11.1" data-path="argparse.html"><a href="argparse.html#创建解析器"><i class="fa fa-check"></i><b>11.1</b> 创建解析器</a></li>
<li class="chapter" data-level="11.2" data-path="argparse.html"><a href="argparse.html#添加参数选项"><i class="fa fa-check"></i><b>11.2</b> 添加参数选项</a><ul>
<li class="chapter" data-level="11.2.1" data-path="argparse.html"><a href="argparse.html#name-or-flags"><i class="fa fa-check"></i><b>11.2.1</b> name or flags</a></li>
<li class="chapter" data-level="11.2.2" data-path="argparse.html"><a href="argparse.html#help"><i class="fa fa-check"></i><b>11.2.2</b> help</a></li>
<li class="chapter" data-level="11.2.3" data-path="argparse.html"><a href="argparse.html#default和type"><i class="fa fa-check"></i><b>11.2.3</b> default和type</a></li>
</ul></li>
<li class="chapter" data-level="11.3" data-path="argparse.html"><a href="argparse.html#参数解析"><i class="fa fa-check"></i><b>11.3</b> 参数解析</a></li>
<li class="chapter" data-level="11.4" data-path="argparse.html"><a href="argparse.html#小结"><i class="fa fa-check"></i><b>11.4</b> 小结</a></li>
<li class="chapter" data-level="11.5" data-path="argparse.html"><a href="argparse.html#参考文献"><i class="fa fa-check"></i><b>11.5</b> 参考文献</a></li>
</ul></li>
<li class="part"><span><b>III 数据抓取</b></span></li>
<li class="chapter" data-level="12" data-path="spider.html"><a href="spider.html"><i class="fa fa-check"></i><b>12</b> 网络爬虫</a><ul>
<li class="chapter" data-level="12.1" data-path="spider.html"><a href="spider.html#网络爬虫原理"><i class="fa fa-check"></i><b>12.1</b> 网络爬虫原理</a></li>
<li class="chapter" data-level="12.2" data-path="spider.html"><a href="spider.html#http请求原理"><i class="fa fa-check"></i><b>12.2</b> HTTP请求原理</a><ul>
<li class="chapter" data-level="12.2.1" data-path="spider.html"><a href="spider.html#url"><i class="fa fa-check"></i><b>12.2.1</b> URL</a></li>
<li class="chapter" data-level="12.2.2" data-path="spider.html"><a href="spider.html#请求方式"><i class="fa fa-check"></i><b>12.2.2</b> 请求方式</a></li>
<li class="chapter" data-level="12.2.3" data-path="spider.html"><a href="spider.html#requests包"><i class="fa fa-check"></i><b>12.2.3</b> requests包</a></li>
</ul></li>
<li class="chapter" data-level="12.3" data-path="spider.html"><a href="spider.html#编码"><i class="fa fa-check"></i><b>12.3</b> 编码</a><ul>
<li class="chapter" data-level="12.3.1" data-path="spider.html"><a href="spider.html#编码方式"><i class="fa fa-check"></i><b>12.3.1</b> 编码方式</a></li>
<li class="chapter" data-level="12.3.2" data-path="spider.html"><a href="spider.html#编码转换"><i class="fa fa-check"></i><b>12.3.2</b> 编码转换</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="spider.html"><a href="spider.html#存储"><i class="fa fa-check"></i><b>12.4</b> 存储</a><ul>
<li class="chapter" data-level="12.4.1" data-path="spider.html"><a href="spider.html#存储到文件"><i class="fa fa-check"></i><b>12.4.1</b> 存储到文件</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="13" data-path="httplib.html"><a href="httplib.html"><i class="fa fa-check"></i><b>13</b> HTTP库</a><ul>
<li class="chapter" data-level="13.1" data-path="httplib.html"><a href="httplib.html#urllib"><i class="fa fa-check"></i><b>13.1</b> urllib</a><ul>
<li class="chapter" data-level="13.1.1" data-path="httplib.html"><a href="httplib.html#发送请求"><i class="fa fa-check"></i><b>13.1.1</b> 发送请求</a></li>
<li class="chapter" data-level="13.1.2" data-path="httplib.html"><a href="httplib.html#处理异常"><i class="fa fa-check"></i><b>13.1.2</b> 处理异常</a></li>
<li class="chapter" data-level="13.1.3" data-path="httplib.html"><a href="httplib.html#解析链接"><i class="fa fa-check"></i><b>13.1.3</b> 解析链接</a></li>
</ul></li>
<li class="chapter" data-level="13.2" data-path="httplib.html"><a href="httplib.html#使用requests"><i class="fa fa-check"></i><b>13.2</b> 使用requests</a><ul>
<li class="chapter" data-level="13.2.1" data-path="httplib.html"><a href="httplib.html#安装requests"><i class="fa fa-check"></i><b>13.2.1</b> 安装requests</a></li>
<li class="chapter" data-level="13.2.2" data-path="httplib.html"><a href="httplib.html#发起请求"><i class="fa fa-check"></i><b>13.2.2</b> 发起请求</a></li>
</ul></li>
<li class="chapter" data-level="13.3" data-path="httplib.html"><a href="httplib.html#网页编码检测及转换"><i class="fa fa-check"></i><b>13.3</b> 网页编码检测及转换</a></li>
<li class="chapter" data-level="13.4" data-path="httplib.html"><a href="httplib.html#参考资料-2"><i class="fa fa-check"></i><b>13.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="pyquery.html"><a href="pyquery.html"><i class="fa fa-check"></i><b>14</b> Pyquery</a><ul>
<li class="chapter" data-level="14.1" data-path="pyquery.html"><a href="pyquery.html#安装"><i class="fa fa-check"></i><b>14.1</b> 安装</a></li>
<li class="chapter" data-level="14.2" data-path="pyquery.html"><a href="pyquery.html#初始化"><i class="fa fa-check"></i><b>14.2</b> 初始化</a></li>
<li class="chapter" data-level="14.3" data-path="pyquery.html"><a href="pyquery.html#获取信息"><i class="fa fa-check"></i><b>14.3</b> 获取信息</a><ul>
<li class="chapter" data-level="14.3.1" data-path="pyquery.html"><a href="pyquery.html#通过选择符选定元素"><i class="fa fa-check"></i><b>14.3.1</b> 通过选择符选定元素</a></li>
<li class="chapter" data-level="14.3.2" data-path="pyquery.html"><a href="pyquery.html#通过迭代获取最终结果"><i class="fa fa-check"></i><b>14.3.2</b> 通过迭代获取最终结果</a></li>
</ul></li>
<li class="chapter" data-level="14.4" data-path="pyquery.html"><a href="pyquery.html#参考资料-3"><i class="fa fa-check"></i><b>14.4</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="databases.html"><a href="databases.html"><i class="fa fa-check"></i><b>15</b> 数据存储</a><ul>
<li class="chapter" data-level="15.1" data-path="databases.html"><a href="databases.html#mysql的存储"><i class="fa fa-check"></i><b>15.1</b> MySQL的存储</a><ul>
<li class="chapter" data-level="15.1.1" data-path="databases.html"><a href="databases.html#安装pymysql和mysql"><i class="fa fa-check"></i><b>15.1.1</b> 安装PyMySQL和MySQL</a></li>
<li class="chapter" data-level="15.1.2" data-path="databases.html"><a href="databases.html#连接数据库"><i class="fa fa-check"></i><b>15.1.2</b> 连接数据库</a></li>
<li class="chapter" data-level="15.1.3" data-path="databases.html"><a href="databases.html#对数据库进行操作"><i class="fa fa-check"></i><b>15.1.3</b> 对数据库进行操作</a></li>
</ul></li>
<li class="chapter" data-level="15.2" data-path="databases.html"><a href="databases.html#mongodb"><i class="fa fa-check"></i><b>15.2</b> MongoDB</a><ul>
<li class="chapter" data-level="15.2.1" data-path="databases.html"><a href="databases.html#在macos中的安装"><i class="fa fa-check"></i><b>15.2.1</b> 在macOS中的安装</a></li>
<li class="chapter" data-level="15.2.2" data-path="databases.html"><a href="databases.html#连接mongodb"><i class="fa fa-check"></i><b>15.2.2</b> 连接MongoDB</a></li>
<li class="chapter" data-level="15.2.3" data-path="databases.html"><a href="databases.html#指定数据库"><i class="fa fa-check"></i><b>15.2.3</b> 指定数据库</a></li>
<li class="chapter" data-level="15.2.4" data-path="databases.html"><a href="databases.html#指定集合"><i class="fa fa-check"></i><b>15.2.4</b> 指定集合</a></li>
<li class="chapter" data-level="15.2.5" data-path="databases.html"><a href="databases.html#插入数据"><i class="fa fa-check"></i><b>15.2.5</b> 插入数据</a></li>
<li class="chapter" data-level="15.2.6" data-path="databases.html"><a href="databases.html#查询数据-1"><i class="fa fa-check"></i><b>15.2.6</b> 查询数据</a></li>
<li class="chapter" data-level="15.2.7" data-path="databases.html"><a href="databases.html#计数-1"><i class="fa fa-check"></i><b>15.2.7</b> 计数</a></li>
<li class="chapter" data-level="15.2.8" data-path="databases.html"><a href="databases.html#排序"><i class="fa fa-check"></i><b>15.2.8</b> 排序</a></li>
<li class="chapter" data-level="15.2.9" data-path="databases.html"><a href="databases.html#偏移和限定"><i class="fa fa-check"></i><b>15.2.9</b> 偏移和限定</a></li>
<li class="chapter" data-level="15.2.10" data-path="databases.html"><a href="databases.html#更新"><i class="fa fa-check"></i><b>15.2.10</b> 更新</a></li>
<li class="chapter" data-level="15.2.11" data-path="databases.html"><a href="databases.html#删除"><i class="fa fa-check"></i><b>15.2.11</b> 删除</a></li>
<li class="chapter" data-level="15.2.12" data-path="databases.html"><a href="databases.html#其他操作"><i class="fa fa-check"></i><b>15.2.12</b> 其他操作</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="databases.html"><a href="databases.html#参考资料-4"><i class="fa fa-check"></i><b>15.3</b> 参考资料</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="selenium.html"><a href="selenium.html"><i class="fa fa-check"></i><b>16</b> 使用Selenium抓取动态渲染页面</a><ul>
<li class="chapter" data-level="16.1" data-path="selenium.html"><a href="selenium.html#安装-1"><i class="fa fa-check"></i><b>16.1</b> 安装</a></li>
<li class="chapter" data-level="16.2" data-path="selenium.html"><a href="selenium.html#selenium的使用"><i class="fa fa-check"></i><b>16.2</b> Selenium的使用</a><ul>
<li class="chapter" data-level="16.2.1" data-path="selenium.html"><a href="selenium.html#初始化浏览器对象"><i class="fa fa-check"></i><b>16.2.1</b> 初始化浏览器对象</a></li>
<li class="chapter" data-level="16.2.2" data-path="selenium.html"><a href="selenium.html#访问页面"><i class="fa fa-check"></i><b>16.2.2</b> 访问页面</a></li>
<li class="chapter" data-level="16.2.3" data-path="selenium.html"><a href="selenium.html#查找节点"><i class="fa fa-check"></i><b>16.2.3</b> 查找节点</a></li>
<li class="chapter" data-level="16.2.4" data-path="selenium.html"><a href="selenium.html#节点操作"><i class="fa fa-check"></i><b>16.2.4</b> 节点操作</a></li>
<li class="chapter" data-level="16.2.5" data-path="selenium.html"><a href="selenium.html#执行javascript"><i class="fa fa-check"></i><b>16.2.5</b> 执行JavaScript</a></li>
<li class="chapter" data-level="16.2.6" data-path="selenium.html"><a href="selenium.html#获取节点信息"><i class="fa fa-check"></i><b>16.2.6</b> 获取节点信息</a></li>
<li class="chapter" data-level="16.2.7" data-path="selenium.html"><a href="selenium.html#延时等待"><i class="fa fa-check"></i><b>16.2.7</b> 延时等待</a></li>
</ul></li>
<li class="chapter" data-level="16.3" data-path="selenium.html"><a href="selenium.html#扩展阅读"><i class="fa fa-check"></i><b>16.3</b> 扩展阅读</a></li>
</ul></li>
<li class="part"><span><b>IV 网络应用开发</b></span></li>
<li class="chapter" data-level="17" data-path="django.html"><a href="django.html"><i class="fa fa-check"></i><b>17</b> Django 框架的安装</a><ul>
<li class="chapter" data-level="17.1" data-path="django.html"><a href="django.html#安装-2"><i class="fa fa-check"></i><b>17.1</b> 安装</a><ul>
<li class="chapter" data-level="17.1.1" data-path="django.html"><a href="django.html#安装-django"><i class="fa fa-check"></i><b>17.1.1</b> 安装 Django</a></li>
<li class="chapter" data-level="17.1.2" data-path="django.html"><a href="django.html#验证安装"><i class="fa fa-check"></i><b>17.1.2</b> 验证安装</a></li>
<li class="chapter" data-level="17.1.3" data-path="django.html"><a href="django.html#创建项目并预览效果"><i class="fa fa-check"></i><b>17.1.3</b> 创建项目并预览效果</a></li>
</ul></li>
<li class="chapter" data-level="17.2" data-path="django.html"><a href="django.html#学习资源-1"><i class="fa fa-check"></i><b>17.2</b> 学习资源</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="quicktutorial.html"><a href="quicktutorial.html"><i class="fa fa-check"></i><b>18</b> 快速上手案例</a><ul>
<li class="chapter" data-level="18.1" data-path="quicktutorial.html"><a href="quicktutorial.html#在-django-项目中创建-app"><i class="fa fa-check"></i><b>18.1</b> 在 Django 项目中创建 APP</a><ul>
<li class="chapter" data-level="18.1.1" data-path="quicktutorial.html"><a href="quicktutorial.html#初始化-app"><i class="fa fa-check"></i><b>18.1.1</b> 初始化 APP</a></li>
<li class="chapter" data-level="18.1.2" data-path="quicktutorial.html"><a href="quicktutorial.html#将应用添加到项目中"><i class="fa fa-check"></i><b>18.1.2</b> 将应用添加到项目中</a></li>
<li class="chapter" data-level="18.1.3" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图"><i class="fa fa-check"></i><b>18.1.3</b> 创建视图</a></li>
<li class="chapter" data-level="18.1.4" data-path="quicktutorial.html"><a href="quicktutorial.html#规划路由"><i class="fa fa-check"></i><b>18.1.4</b> 规划路由</a></li>
<li class="chapter" data-level="18.1.5" data-path="quicktutorial.html"><a href="quicktutorial.html#预览效果"><i class="fa fa-check"></i><b>18.1.5</b> 预览效果</a></li>
</ul></li>
<li class="chapter" data-level="18.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模型的创建及使用"><i class="fa fa-check"></i><b>18.2</b> 模型的创建及使用</a><ul>
<li class="chapter" data-level="18.2.1" data-path="quicktutorial.html"><a href="quicktutorial.html#配置项目信息"><i class="fa fa-check"></i><b>18.2.1</b> 配置项目信息</a></li>
<li class="chapter" data-level="18.2.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型"><i class="fa fa-check"></i><b>18.2.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.2.3" data-path="quicktutorial.html"><a href="quicktutorial.html#激活模型"><i class="fa fa-check"></i><b>18.2.3</b> 激活模型</a></li>
<li class="chapter" data-level="18.2.4" data-path="quicktutorial.html"><a href="quicktutorial.html#数据库的生成与迁移"><i class="fa fa-check"></i><b>18.2.4</b> 数据库的生成与迁移</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="quicktutorial.html"><a href="quicktutorial.html#后台管理"><i class="fa fa-check"></i><b>18.3</b> 后台管理</a><ul>
<li class="chapter" data-level="18.3.1" data-path="quicktutorial.html"><a href="quicktutorial.html#创建管理员账号"><i class="fa fa-check"></i><b>18.3.1</b> 创建管理员账号</a></li>
<li class="chapter" data-level="18.3.2" data-path="quicktutorial.html"><a href="quicktutorial.html#添加引用到后台管理"><i class="fa fa-check"></i><b>18.3.2</b> 添加引用到后台管理</a></li>
<li class="chapter" data-level="18.3.3" data-path="quicktutorial.html"><a href="quicktutorial.html#进入后台管理界面"><i class="fa fa-check"></i><b>18.3.3</b> 进入后台管理界面</a></li>
</ul></li>
<li class="chapter" data-level="18.4" data-path="quicktutorial.html"><a href="quicktutorial.html#创建视图-1"><i class="fa fa-check"></i><b>18.4</b> 创建视图</a><ul>
<li class="chapter" data-level="18.4.1" data-path="quicktutorial.html"><a href="quicktutorial.html#添加多个视图"><i class="fa fa-check"></i><b>18.4.1</b> 添加多个视图</a></li>
<li class="chapter" data-level="18.4.2" data-path="quicktutorial.html"><a href="quicktutorial.html#模版命名空间"><i class="fa fa-check"></i><b>18.4.2</b> 模版命名空间</a></li>
<li class="chapter" data-level="18.4.3" data-path="quicktutorial.html"><a href="quicktutorial.html#在视图中使用模板"><i class="fa fa-check"></i><b>18.4.3</b> 在视图中使用模板</a></li>
<li class="chapter" data-level="18.4.4" data-path="quicktutorial.html"><a href="quicktutorial.html#url命名空间"><i class="fa fa-check"></i><b>18.4.4</b> URL命名空间</a></li>
</ul></li>
<li class="chapter" data-level="18.5" data-path="quicktutorial.html"><a href="quicktutorial.html#模型"><i class="fa fa-check"></i><b>18.5</b> 模型</a><ul>
<li class="chapter" data-level="18.5.1" data-path="quicktutorial.html"><a href="quicktutorial.html#设置数据库信息-1"><i class="fa fa-check"></i><b>18.5.1</b> 设置数据库信息</a></li>
<li class="chapter" data-level="18.5.2" data-path="quicktutorial.html"><a href="quicktutorial.html#创建模型-1"><i class="fa fa-check"></i><b>18.5.2</b> 创建模型</a></li>
<li class="chapter" data-level="18.5.3" data-path="quicktutorial.html"><a href="quicktutorial.html#使用模型"><i class="fa fa-check"></i><b>18.5.3</b> 使用模型</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="quicktutorial.html"><a href="quicktutorial.html#参考文献-1"><i class="fa fa-check"></i><b>18.6</b> 参考文献</a></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Python 编程</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="databases" class="section level1">
<h1><span class="header-section-number">第 15 章</span> 数据存储</h1>
<p>获取到的信息，我们通常使用文件、数据库的形式存储起来，以便再次利用。</p>
<p>使用文件的方式保存数据，用法比较简单，而数据库的方式则分为使用关系型数据库和非关系型数据库，其各自的代表数据库产品是MySQL和MongoDB，皆为开源产品，从性能和安全性角度都具有很强的竞争力。因而，下面介绍这两个数据库在Python中的使用。</p>
<div id="mysql的存储" class="section level2">
<h2><span class="header-section-number">15.1</span> MySQL的存储</h2>
<p>成功从页面获取信息后，我们可以将其保存到文件中，也可以存储到数据库中。显然，使用数据库拥有更多后续分析的便利，我们采用常见的关系型数据库MySQL作为信息的保存方式。</p>
<div id="安装pymysql和mysql" class="section level3">
<h3><span class="header-section-number">15.1.1</span> 安装PyMySQL和MySQL</h3>
<p>使用Homebrew和pip工具分别安装MySQL和PyMySQL。</p>
</div>
<div id="连接数据库" class="section level3">
<h3><span class="header-section-number">15.1.2</span> 连接数据库</h3>
<p>对数据库的使用和其它语言是一致的，无非就是连接数据库，发送指令，接收结果。</p>
<div class="sourceCode" id="cb246"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb246-1"><a href="databases.html#cb246-1"></a><span class="im">import</span> pymysql</span>
<span id="cb246-2"><a href="databases.html#cb246-2"></a>db <span class="op">=</span> pymysql.<span class="ex">connect</span>(host<span class="op">=</span><span class="st">&#39;localhost&#39;</span>, user<span class="op">=</span><span class="st">&#39;user&#39;</span>,password<span class="op">=</span><span class="st">&#39;password&#39;</span>, port<span class="op">=</span><span class="dv">3306</span>)</span></code></pre></div>
<p>使用pymysql的<code>connect()</code>方法，设置必要的信息，可以连接到MySQL数据库。</p>
<p>还可以直接连接到MySQL中已经存在的库：</p>
<div class="sourceCode" id="cb247"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb247-1"><a href="databases.html#cb247-1"></a>connection <span class="op">=</span> pymysql.<span class="ex">connect</span>(host<span class="op">=</span><span class="st">&#39;localhost&#39;</span>,</span>
<span id="cb247-2"><a href="databases.html#cb247-2"></a>                             user<span class="op">=</span><span class="st">&#39;user&#39;</span>,</span>
<span id="cb247-3"><a href="databases.html#cb247-3"></a>                             password<span class="op">=</span><span class="st">&#39;passwd&#39;</span>,</span>
<span id="cb247-4"><a href="databases.html#cb247-4"></a>                             db<span class="op">=</span><span class="st">&#39;db&#39;</span>,</span>
<span id="cb247-5"><a href="databases.html#cb247-5"></a>                             charset<span class="op">=</span><span class="st">&#39;utf8mb4&#39;</span>)</span></code></pre></div>
</div>
<div id="对数据库进行操作" class="section level3">
<h3><span class="header-section-number">15.1.3</span> 对数据库进行操作</h3>
<p>连接到数据库后，可以使用游标cursor()方法对数据库进行各种操作。</p>
<div id="创建数据库" class="section level4">
<h4><span class="header-section-number">15.1.3.1</span> 创建数据库</h4>
<p>使用SQL语句，就可以创建数据库，创建数据库的语句如下：</p>
<div class="sourceCode" id="cb248"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb248-1"><a href="databases.html#cb248-1"></a><span class="im">import</span> pymysql</span>
<span id="cb248-2"><a href="databases.html#cb248-2"></a></span>
<span id="cb248-3"><a href="databases.html#cb248-3"></a>db <span class="op">=</span> pymysql.<span class="ex">connect</span>(host<span class="op">=</span><span class="st">&#39;localhost&#39;</span>, user<span class="op">=</span><span class="st">&#39;root&#39;</span>,</span>
<span id="cb248-4"><a href="databases.html#cb248-4"></a>                     password<span class="op">=</span><span class="st">&#39;mysql&#39;</span>, port<span class="op">=</span><span class="dv">3306</span>)</span>
<span id="cb248-5"><a href="databases.html#cb248-5"></a></span>
<span id="cb248-6"><a href="databases.html#cb248-6"></a>cursor <span class="op">=</span> db.cursor()</span>
<span id="cb248-7"><a href="databases.html#cb248-7"></a>sql <span class="op">=</span> <span class="st">&quot;CREATE DATABASE spiders DEFAULT CHARACTER SET utf8&quot;</span></span>
<span id="cb248-8"><a href="databases.html#cb248-8"></a>cursor.execute(sql)</span>
<span id="cb248-9"><a href="databases.html#cb248-9"></a>db.commit()</span>
<span id="cb248-10"><a href="databases.html#cb248-10"></a>db.close()</span></code></pre></div>
<p>使用游标方法<code>cursor()</code>创建游标对象，用<code>execute()</code>方法执行SQL语句，再使用<code>commit()</code>提交，最后使用<code>close()</code>方法关闭数据库连接。</p>
</div>
<div id="创建数据表" class="section level4">
<h4><span class="header-section-number">15.1.3.2</span> 创建数据表</h4>
<p>使用SQL语句可以创建数据表，当然前提是连接到数据库。</p>
<div class="sourceCode" id="cb249"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb249-1"><a href="databases.html#cb249-1"></a><span class="co"># coding=utf-8</span></span>
<span id="cb249-2"><a href="databases.html#cb249-2"></a><span class="im">import</span> pymysql</span>
<span id="cb249-3"><a href="databases.html#cb249-3"></a></span>
<span id="cb249-4"><a href="databases.html#cb249-4"></a>db <span class="op">=</span> pymysql.<span class="ex">connect</span>(host<span class="op">=</span><span class="st">&#39;localhost&#39;</span>, user<span class="op">=</span><span class="st">&#39;root&#39;</span>,</span>
<span id="cb249-5"><a href="databases.html#cb249-5"></a>                     password<span class="op">=</span><span class="st">&#39;mysql&#39;</span>, db<span class="op">=</span><span class="st">&#39;spiders&#39;</span>)</span>
<span id="cb249-6"><a href="databases.html#cb249-6"></a></span>
<span id="cb249-7"><a href="databases.html#cb249-7"></a>cursor <span class="op">=</span> db.cursor()</span>
<span id="cb249-8"><a href="databases.html#cb249-8"></a>sql <span class="op">=</span> <span class="st">&quot;CREATE TABLE IF NOT EXISTS students (id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (id))&quot;</span></span>
<span id="cb249-9"><a href="databases.html#cb249-9"></a>cursor.execute(sql)</span>
<span id="cb249-10"><a href="databases.html#cb249-10"></a>db.commit()</span>
<span id="cb249-11"><a href="databases.html#cb249-11"></a></span>
<span id="cb249-12"><a href="databases.html#cb249-12"></a>db.close()</span></code></pre></div>
<p>SQL语句的语法，可以参照MySQL参考手册，也可借助于类似phpMyadmin之类的工具进行生成和检测。</p>
</div>
<div id="插入更新删除数据" class="section level4">
<h4><span class="header-section-number">15.1.3.3</span> 插入、更新、删除数据</h4>
<p>使用INSERT语句，可以插入数据到数据表：</p>
<div class="sourceCode" id="cb250"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb250-1"><a href="databases.html#cb250-1"></a><span class="co"># coding=utf-8</span></span>
<span id="cb250-2"><a href="databases.html#cb250-2"></a><span class="im">import</span> pymysql</span>
<span id="cb250-3"><a href="databases.html#cb250-3"></a></span>
<span id="cb250-4"><a href="databases.html#cb250-4"></a>db <span class="op">=</span> pymysql.<span class="ex">connect</span>(host<span class="op">=</span><span class="st">&#39;localhost&#39;</span>, user<span class="op">=</span><span class="st">&#39;root&#39;</span>,</span>
<span id="cb250-5"><a href="databases.html#cb250-5"></a>                     password<span class="op">=</span><span class="st">&#39;mysql&#39;</span>, db<span class="op">=</span><span class="st">&#39;spiders&#39;</span>)</span>
<span id="cb250-6"><a href="databases.html#cb250-6"></a></span>
<span id="cb250-7"><a href="databases.html#cb250-7"></a>cursor <span class="op">=</span> db.cursor()</span>
<span id="cb250-8"><a href="databases.html#cb250-8"></a></span>
<span id="cb250-9"><a href="databases.html#cb250-9"></a><span class="bu">id</span> <span class="op">=</span> <span class="st">&#39;2018&#39;</span></span>
<span id="cb250-10"><a href="databases.html#cb250-10"></a>user <span class="op">=</span> <span class="st">&#39;yangjh&#39;</span></span>
<span id="cb250-11"><a href="databases.html#cb250-11"></a>age <span class="op">=</span> <span class="dv">18</span></span>
<span id="cb250-12"><a href="databases.html#cb250-12"></a></span>
<span id="cb250-13"><a href="databases.html#cb250-13"></a>sql <span class="op">=</span> <span class="st">&#39;INSERT INTO students(id,name,age) values(</span><span class="sc">%s</span><span class="st">,</span><span class="sc">%s</span><span class="st">,</span><span class="sc">%s</span><span class="st">)&#39;</span></span>
<span id="cb250-14"><a href="databases.html#cb250-14"></a><span class="cf">try</span>:</span>
<span id="cb250-15"><a href="databases.html#cb250-15"></a>    cursor.execute(sql, (<span class="bu">id</span>, user, age))</span>
<span id="cb250-16"><a href="databases.html#cb250-16"></a>    db.commit()</span>
<span id="cb250-17"><a href="databases.html#cb250-17"></a><span class="cf">except</span>:</span>
<span id="cb250-18"><a href="databases.html#cb250-18"></a>    db.rollback()</span>
<span id="cb250-19"><a href="databases.html#cb250-19"></a>db.close()</span></code></pre></div>
<p>插入、更新和删除操作都是对数据库进行更改的操作，这些操作的写法是：</p>
<div class="sourceCode" id="cb251"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb251-1"><a href="databases.html#cb251-1"></a><span class="cf">try</span>:</span>
<span id="cb251-2"><a href="databases.html#cb251-2"></a>    cursor.execute(sql)</span>
<span id="cb251-3"><a href="databases.html#cb251-3"></a>    db.commit()</span>
<span id="cb251-4"><a href="databases.html#cb251-4"></a><span class="cf">except</span>:</span>
<span id="cb251-5"><a href="databases.html#cb251-5"></a>    db.rollback()</span></code></pre></div>
<p>使用rollback()方法可以回滚执行失败的操作，保持事务的原子性（atomicity），使用异常处理，可以方便地实现业务回滚。</p>
</div>
<div id="查询数据" class="section level4">
<h4><span class="header-section-number">15.1.3.4</span> 查询数据</h4>
<p>使用<code>while</code>循环加<code>fetchone()</code>方法循环获取数据：</p>
<div class="sourceCode" id="cb252"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb252-1"><a href="databases.html#cb252-1"></a>sql <span class="op">=</span> <span class="st">&#39;SELECT * FROM students WHERE age &lt;=20&#39;</span></span>
<span id="cb252-2"><a href="databases.html#cb252-2"></a><span class="cf">try</span>:</span>
<span id="cb252-3"><a href="databases.html#cb252-3"></a>    cursor.execute(sql)</span>
<span id="cb252-4"><a href="databases.html#cb252-4"></a>    row <span class="op">=</span> cursor.fetchone()</span>
<span id="cb252-5"><a href="databases.html#cb252-5"></a>    <span class="cf">while</span> row:</span>
<span id="cb252-6"><a href="databases.html#cb252-6"></a>        <span class="bu">print</span>(row, row[<span class="dv">0</span>])</span>
<span id="cb252-7"><a href="databases.html#cb252-7"></a>        row <span class="op">=</span> cursor.fetchone()</span>
<span id="cb252-8"><a href="databases.html#cb252-8"></a><span class="cf">except</span>:</span>
<span id="cb252-9"><a href="databases.html#cb252-9"></a>    <span class="bu">print</span>(<span class="st">&#39;出错啦！&#39;</span>)</span>
<span id="cb252-10"><a href="databases.html#cb252-10"></a>db.close()</span></code></pre></div>
</div>
</div>
</div>
<div id="mongodb" class="section level2">
<h2><span class="header-section-number">15.2</span> MongoDB</h2>
<p>MongoDB 是一款开源的非关系数据库管理软件，性能优异，在诸多大型项目中表现良好，是非关系型数据库的首选。</p>
<div id="在macos中的安装" class="section level3">
<h3><span class="header-section-number">15.2.1</span> 在macOS中的安装</h3>
<p>MongoDB 只支持macOS 10.11及以后的产品。</p>
<div id="使用包管理工具安装" class="section level4">
<h4><span class="header-section-number">15.2.1.1</span> 使用包管理工具安装</h4>
<p>我们使用在macOS 中的包管理工具Homebrew进行安装。</p>
<div class="sourceCode" id="cb253"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb253-1"><a href="databases.html#cb253-1"></a><span class="ex">brew</span> install mongodb</span></code></pre></div>
<p>上述指令将安装MongoDB的最新发行版，并会自动安装MongoDB所需要的依赖环境。</p>
</div>
<div id="运行前的准备" class="section level4">
<h4><span class="header-section-number">15.2.1.2</span> 运行前的准备</h4>
<p>在启动MongoDB之前，需要创建MongoDB读写数据的目录，默认情况下，MongoDB会以<code>/data/db</code>为默认目录。下面的命令将创建默认的<code>/data/db</code>目录：</p>
<div class="sourceCode" id="cb254"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb254-1"><a href="databases.html#cb254-1"></a><span class="fu">mkdir</span> -p /data/db</span></code></pre></div>
<p>创建目录后，为给目录指定读写权限。然后使用如下命令将MongoDB加入到系统服务中，以便每次启动时都可自动启动MongoDB服务。</p>
<div class="sourceCode" id="cb255"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb255-1"><a href="databases.html#cb255-1"></a><span class="ex">brew</span> services start mongodb</span></code></pre></div>
</div>
<div id="安装pymongo库" class="section level4">
<h4><span class="header-section-number">15.2.1.3</span> 安装PyMongo库</h4>
<p>启动虚拟python环境，然后通过pip安装PyMongo库：</p>
<div class="sourceCode" id="cb256"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb256-1"><a href="databases.html#cb256-1"></a> 👉  <span class="bu">source</span> bin/activate</span>
<span id="cb256-2"><a href="databases.html#cb256-2"></a><span class="kw">(</span><span class="ex">spider</span><span class="kw">)</span> </span>
<span id="cb256-3"><a href="databases.html#cb256-3"></a> 👉  <span class="ex">pip3</span> install pymongo</span>
<span id="cb256-4"><a href="databases.html#cb256-4"></a><span class="ex">Collecting</span> pymongo</span>
<span id="cb256-5"><a href="databases.html#cb256-5"></a>  <span class="ex">Downloading</span> https://files.pythonhosted.org/packages/d7/ac/d2e324c1f9bcf653fa106785371a16b4709506a35b04948655de8b961a85/pymongo-3.7.2-cp37-cp37m-macosx_10_9_x86_64.whl (307kB)</span>
<span id="cb256-6"><a href="databases.html#cb256-6"></a>    <span class="ex">100%</span> <span class="kw">|</span>████████████████████████████████<span class="kw">|</span> <span class="ex">317kB</span> 1.6MB/s </span>
<span id="cb256-7"><a href="databases.html#cb256-7"></a><span class="ex">Installing</span> collected packages: pymongo</span>
<span id="cb256-8"><a href="databases.html#cb256-8"></a><span class="ex">Successfully</span> installed pymongo-3.7.2</span></code></pre></div>
</div>
<div id="安装robo-3t" class="section level4">
<h4><span class="header-section-number">15.2.1.4</span> 安装Robo 3T</h4>
<p>MongoDB并没有默认的图形化管理工具，只提供Mongo shell的命令行方式。为了方便，我们还可以使用诸如Robo 3T这样的图形化管理工具进行查询、修改等管理工作。使用Homebrew安装：</p>
<div class="sourceCode" id="cb257"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb257-1"><a href="databases.html#cb257-1"></a><span class="ex">brew</span> install robo-3t</span></code></pre></div>
<p>安装后，在应用程序中找到robo-3t，进行必要的配置（指定主机名和端口），即可对MongoDB进行管理。</p>
</div>
</div>
<div id="连接mongodb" class="section level3">
<h3><span class="header-section-number">15.2.2</span> 连接MongoDB</h3>
<p>使用PyMongo库，可以在Python中对MongoDB进行操作，连接到数据库，使用MongoClient方法即可：</p>
<div class="sourceCode" id="cb258"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb258-1"><a href="databases.html#cb258-1"></a><span class="im">import</span> pymongo</span>
<span id="cb258-2"><a href="databases.html#cb258-2"></a></span>
<span id="cb258-3"><a href="databases.html#cb258-3"></a>client <span class="op">=</span> pymongo.MongoClient(<span class="st">&#39;mongodb://localhost:27017&#39;</span>)</span></code></pre></div>
</div>
<div id="指定数据库" class="section level3">
<h3><span class="header-section-number">15.2.3</span> 指定数据库</h3>
<p>MongoDB中建立和使用数据库非常简单，直接指定数据库名称即可，如果该数据库不存在，MongoDB就会创建该数据库：</p>
<div class="sourceCode" id="cb259"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb259-1"><a href="databases.html#cb259-1"></a>db <span class="op">=</span> client[<span class="st">&#39;weibo&#39;</span>]</span></code></pre></div>
</div>
<div id="指定集合" class="section level3">
<h3><span class="header-section-number">15.2.4</span> 指定集合</h3>
<p>MongoDB中的集合（collection），相当于MySQL等关系型数据库中的数据表（table），具体的信息，必须指定一个集合，才能进行存入、修改等操作。使用Collection对象，即可指定集合：</p>
<div class="sourceCode" id="cb260"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb260-1"><a href="databases.html#cb260-1"></a><span class="im">import</span> pymongo</span>
<span id="cb260-2"><a href="databases.html#cb260-2"></a>client <span class="op">=</span> pymongo.MongoClient(<span class="st">&#39;mongodb://localhost:27017&#39;</span>)</span>
<span id="cb260-3"><a href="databases.html#cb260-3"></a>db <span class="op">=</span> client[<span class="st">&#39;weibo&#39;</span>]</span>
<span id="cb260-4"><a href="databases.html#cb260-4"></a>collection <span class="op">=</span> db[<span class="st">&#39;yangjh&#39;</span>]</span></code></pre></div>
</div>
<div id="插入数据" class="section level3">
<h3><span class="header-section-number">15.2.5</span> 插入数据</h3>
<p>推荐使用<code>insert_one()</code>和<code>insert_many()</code>方法来分别插入单条记录和多条记录。</p>
<div class="sourceCode" id="cb261"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb261-1"><a href="databases.html#cb261-1"></a>card <span class="op">=</span> {</span>
<span id="cb261-2"><a href="databases.html#cb261-2"></a>    <span class="st">&quot;id&quot;</span>: <span class="st">&quot;20183210&quot;</span>,</span>
<span id="cb261-3"><a href="databases.html#cb261-3"></a>    <span class="st">&quot;name&quot;</span>: <span class="st">&#39;yangjh&#39;</span>,</span>
<span id="cb261-4"><a href="databases.html#cb261-4"></a>    <span class="st">&quot;age&quot;</span>: <span class="dv">20</span>,</span>
<span id="cb261-5"><a href="databases.html#cb261-5"></a>    <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;male&#39;</span></span>
<span id="cb261-6"><a href="databases.html#cb261-6"></a>}</span>
<span id="cb261-7"><a href="databases.html#cb261-7"></a></span>
<span id="cb261-8"><a href="databases.html#cb261-8"></a>result <span class="op">=</span> collection.insert_one(card)</span>
<span id="cb261-9"><a href="databases.html#cb261-9"></a><span class="bu">print</span>(result)</span>
<span id="cb261-10"><a href="databases.html#cb261-10"></a><span class="bu">print</span>(result.inserted_id)</span></code></pre></div>
<p>运行结果为：</p>
<div class="sourceCode" id="cb262"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb262-1"><a href="databases.html#cb262-1"></a><span class="op">&lt;</span><span class="ex">pymongo.results.InsertOneResult</span> object at 0x10436da48<span class="op">&gt;</span></span>
<span id="cb262-2"><a href="databases.html#cb262-2"></a><span class="ex">5bf8b3f897c32c4128ae8f6f</span></span></code></pre></div>
<p><code>insert_one()</code>方法返回一个对象，调用其<code>inserted_id</code>属性，可以获得<code>_id</code>字段的值。</p>
<p>还可以使用<code>insert_many()</code>方法，一次插入多条记录：</p>
<div class="sourceCode" id="cb263"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb263-1"><a href="databases.html#cb263-1"></a>card1 <span class="op">=</span> {</span>
<span id="cb263-2"><a href="databases.html#cb263-2"></a>    <span class="st">&quot;id&quot;</span>: <span class="st">&quot;20183211&quot;</span>,</span>
<span id="cb263-3"><a href="databases.html#cb263-3"></a>    <span class="st">&quot;name&quot;</span>: <span class="st">&#39;yangzh&#39;</span>,</span>
<span id="cb263-4"><a href="databases.html#cb263-4"></a>    <span class="st">&quot;age&quot;</span>: <span class="dv">20</span>,</span>
<span id="cb263-5"><a href="databases.html#cb263-5"></a>    <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;male&#39;</span></span>
<span id="cb263-6"><a href="databases.html#cb263-6"></a>}</span>
<span id="cb263-7"><a href="databases.html#cb263-7"></a></span>
<span id="cb263-8"><a href="databases.html#cb263-8"></a>card2 <span class="op">=</span> {</span>
<span id="cb263-9"><a href="databases.html#cb263-9"></a>    <span class="st">&quot;id&quot;</span>: <span class="st">&quot;20183212&quot;</span>,</span>
<span id="cb263-10"><a href="databases.html#cb263-10"></a>    <span class="st">&quot;name&quot;</span>: <span class="st">&#39;yangzhh&#39;</span>,</span>
<span id="cb263-11"><a href="databases.html#cb263-11"></a>    <span class="st">&quot;age&quot;</span>: <span class="dv">40</span>,</span>
<span id="cb263-12"><a href="databases.html#cb263-12"></a>    <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;female&#39;</span></span>
<span id="cb263-13"><a href="databases.html#cb263-13"></a>}</span>
<span id="cb263-14"><a href="databases.html#cb263-14"></a>result <span class="op">=</span> collection.insert_many([card1, card2])</span>
<span id="cb263-15"><a href="databases.html#cb263-15"></a><span class="bu">print</span>(result)</span>
<span id="cb263-16"><a href="databases.html#cb263-16"></a><span class="bu">print</span>(result.inserted_ids)</span></code></pre></div>
</div>
<div id="查询数据-1" class="section level3">
<h3><span class="header-section-number">15.2.6</span> 查询数据</h3>
<p>查询数据，使用<code>find_one()</code>或<code>find()</code>方法进行查询，顾名思义，<code>find_one()</code>方法查询结果是单条，而<code>find()</code>方法返回全部结果。</p>
<div class="sourceCode" id="cb264"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb264-1"><a href="databases.html#cb264-1"></a>results <span class="op">=</span> collection.find({<span class="st">&#39;age&#39;</span>: <span class="dv">20</span>})</span>
<span id="cb264-2"><a href="databases.html#cb264-2"></a><span class="bu">print</span>(results)</span>
<span id="cb264-3"><a href="databases.html#cb264-3"></a><span class="cf">for</span> result <span class="kw">in</span> results:</span>
<span id="cb264-4"><a href="databases.html#cb264-4"></a>    <span class="bu">print</span>(result)</span></code></pre></div>
<p>运行结果为：</p>
<div class="sourceCode" id="cb265"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb265-1"><a href="databases.html#cb265-1"></a><span class="op">&lt;</span><span class="ex">pymongo.cursor.Cursor</span> object at 0x103afe550<span class="op">&gt;</span></span>
<span id="cb265-2"><a href="databases.html#cb265-2"></a>{<span class="st">&#39;_id&#39;</span>: <span class="ex">ObjectId</span>(<span class="st">&#39;5bf8b3f897c32c4128ae8f6f&#39;</span>), <span class="st">&#39;id&#39;</span>: <span class="st">&#39;20183210&#39;</span>, <span class="st">&#39;name&#39;</span>: <span class="st">&#39;yangjh&#39;</span>, <span class="st">&#39;age&#39;</span>: <span class="ex">20</span>, <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;male&#39;</span>}</span>
<span id="cb265-3"><a href="databases.html#cb265-3"></a>{<span class="st">&#39;_id&#39;</span>: <span class="ex">ObjectId</span>(<span class="st">&#39;5bf8b81c97c32c433d302e60&#39;</span>), <span class="st">&#39;id&#39;</span>: <span class="st">&#39;20183210&#39;</span>, <span class="st">&#39;name&#39;</span>: <span class="st">&#39;yangjh&#39;</span>, <span class="st">&#39;age&#39;</span>: <span class="ex">20</span>, <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;male&#39;</span>}</span>
<span id="cb265-4"><a href="databases.html#cb265-4"></a>{<span class="st">&#39;_id&#39;</span>: <span class="ex">ObjectId</span>(<span class="st">&#39;5bf8b81c97c32c433d302e61&#39;</span>), <span class="st">&#39;id&#39;</span>: <span class="st">&#39;20183211&#39;</span>, <span class="st">&#39;name&#39;</span>: <span class="st">&#39;yangzh&#39;</span>, <span class="st">&#39;age&#39;</span>: <span class="ex">20</span>, <span class="st">&#39;gender&#39;</span>: <span class="st">&#39;male&#39;</span>}</span></code></pre></div>
<p>可以看出，查询结果为Cursor类型，是一个生成器，需要遍历才能获取其中所有的结果。</p>
<p>在使用find()或find_one()方法时，需要以对象的形式传入查询条件。常见的查询条件总结如下：</p>
<table>
<colgroup>
<col width="14%" />
<col width="28%" />
<col width="57%" />
</colgroup>
<thead>
<tr class="header">
<th>符号</th>
<th>含义</th>
<th>示例</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>eq</code></td>
<td>等于</td>
<td><code>{'age':20}</code></td>
</tr>
<tr class="even">
<td><code>lt</code></td>
<td>小于</td>
<td><code>{'age':{'$lt':20}}</code></td>
</tr>
<tr class="odd">
<td><code>gt</code></td>
<td>大于</td>
<td><code>{'age':{'$gt':20}}</code></td>
</tr>
<tr class="even">
<td><code>lte</code></td>
<td>小于等于</td>
<td><code>{'age':{'$lte':20}}</code></td>
</tr>
<tr class="odd">
<td><code>gte</code></td>
<td>大于等于</td>
<td>{‘age’:{‘<span class="math inline">\(gte&#39;:20}} | | `ne` | 不等于 | {&#39;age&#39;:{&#39;\)</span>ne’:20}}</td>
</tr>
<tr class="even">
<td><code>in</code></td>
<td>在范围内</td>
<td>{‘age’:{‘<span class="math inline">\(in&#39;:[20,30]}} | | `nin` | 不在范围内 | {&#39;age&#39;:{&#39;\)</span>nin’:[20,30]}}</td>
</tr>
<tr class="odd">
<td><code>regex</code></td>
<td>匹配正则表达式</td>
<td>{‘name’:{‘<span class="math inline">\(regex&#39;:&#39;^yangzh&#39;}} | | `exists` | 属性是否存在 | {&#39;age&#39;:{&#39;\)</span>exists’:True}}</td>
</tr>
<tr class="even">
<td><code>type</code></td>
<td>类型判断</td>
<td>{‘age’:{‘<span class="math inline">\(type&#39;:&#39;int&#39;}} | | `text` | 文本查询 | {&#39;\)</span>text’:{‘<span class="math inline">\(search&#39;:&#39;yang&#39;}} | | `where` | 高级条件查询 | {&#39;\)</span>where’:‘python表达式’}</td>
</tr>
</tbody>
</table>
</div>
<div id="计数-1" class="section level3">
<h3><span class="header-section-number">15.2.7</span> 计数</h3>
<p>可以使用<code>count()</code>方法统计查询结果的数量。</p>
</div>
<div id="排序" class="section level3">
<h3><span class="header-section-number">15.2.8</span> 排序</h3>
<p>使用<code>sort()</code>方法进行排序，排序时需要传入字段名及升降序标记，如：</p>
<div class="sourceCode" id="cb266"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb266-1"><a href="databases.html#cb266-1"></a>results <span class="op">=</span> collection.find().sort(<span class="st">&#39;name&#39;</span>)</span></code></pre></div>
<p>将以name字段为依据，按照字母升序进行排序。若要倒序，则传入<code>pymongo.DESCENDING</code>。</p>
</div>
<div id="偏移和限定" class="section level3">
<h3><span class="header-section-number">15.2.9</span> 偏移和限定</h3>
<p>使用<code>skip()</code>方法，可以从指定的位置获取查询结果。使用<code>limit()</code>方法，可以限定返回的数量结果。</p>
</div>
<div id="更新" class="section level3">
<h3><span class="header-section-number">15.2.10</span> 更新</h3>
<p>使用<code>update_one()</code>方法和<code>update_many()</code>方法，可以更新符合条件的数据。</p>
</div>
<div id="删除" class="section level3">
<h3><span class="header-section-number">15.2.11</span> 删除</h3>
<p>使用<code>delete_one()</code>方法和<code>delete_many()</code>方法，可以删除符合条件的数据。</p>
</div>
<div id="其他操作" class="section level3">
<h3><span class="header-section-number">15.2.12</span> 其他操作</h3>
<p>还可以使用类似<code>fine_one_and_delete()</code>的方法，对数据进行操作。</p>
</div>
</div>
<div id="参考资料-4" class="section level2">
<h2><span class="header-section-number">15.3</span> 参考资料</h2>
<ol style="list-style-type: decimal">
<li><a href="https://dev.mysql.com/doc/">MySQL手册</a></li>
<li><a href="https://pymysql.readthedocs.io/en/latest/index.html">pymysql文档</a></li>
<li><a href="https://docs.mongodb.com/">https://docs.mongodb.com/</a></li>
<li><a href="https://api.mongodb.com/python/current/">https://api.mongodb.com/python/current/</a></li>
<li><a href="https://docs.mongodb.com/manual/reference/operator/query/">MongoDB查询操作符手册</a></li>
</ol>

</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="pyquery.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="selenium.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/yangjh-xbmu/learningpython/edit/master/1500-spider-storage.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["learningpython.pdf", "learningpython.epub"],
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
